﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class D08_Touch : MonoBehaviour
{
    public Transform centerTf;
    private float startMag;//两个手指刚开始的距离
    private Vector3 startPos;//两指开始的摄像机位置
    //获取触摸
    private void Update1()
    {
        int touchCount = Input.touchCount;//当前有几根手指在触摸屏幕
        if (touchCount > 0)
        {
            if (touchCount == 1)//单指触摸
            {
                Touch touch0 = Input.GetTouch(0);//获取第0根手指的触摸信息
                float deltaX = touch0.deltaPosition.x;
                float deltaY = touch0.deltaPosition.y;
                transform.RotateAround(centerTf.position, Vector3.up, 10 * Time.deltaTime * deltaX);
                transform.RotateAround(centerTf.position, transform.right, -10 * Time.deltaTime * deltaY);
            }
            else if (touchCount == 2)//双指触摸缩放
            {
                Touch touch0 = Input.GetTouch(0);
                Touch touch1 = Input.GetTouch(1);
                if (touch1.phase == TouchPhase.Began)//两根手指开始
                {
                    startMag = (touch0.position - touch1.position).magnitude;//两个手指刚开始的距离
                    startPos = transform.position;
                }
                else
                {
                    float currentMag = (touch0.position - touch1.position).magnitude;//当前两根手指的距离
                    float deltaMag = currentMag - startMag;//手指距离相对初始距离的变化量
                    //改变摄像机位置
                    transform.position = startPos + transform.forward * deltaMag * 0.01f;
                }
            }
        }
    }

    private void Update()
    {
        //加速度
        print(Input.acceleration);
    }
}